//
//  MCActionMachineryVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/17.
//

import UIKit
import QMUIKit
import Toast_Swift
import SnapKit
import ObjectMapper
import Kingfisher


class MCActionMachineryVc: MCBaseVc {

    let actionPictureCollectionViewIdentifier = "actionPictureCollectionViewIdentifier"
    
    let MCActionRecommandCollectionCellIdentifier = "MCActionRecommandCollectionCellIdentifier"
    
    let MCIntrumentRecommandCollectionCellIdentifier = "MCIntrumentRecommandCollectionCellIdentifier"
    
    var ids:String = ""
    
    @IBOutlet weak var playerScorllview: UIView!
    
    @IBOutlet weak var pictureCollectionView: UICollectionView!
    
    
    //英文标题
    @IBOutlet weak var titleEnLabel:UILabel!
    
    //中文标题
    @IBOutlet weak var titleCnLabel:UILabel!
    
    //收藏标签
    @IBOutlet weak var collectionImageBtn:UIButton!
    
    //分享标签
    @IBOutlet weak var shareLabelBtn:UIButton!
    
    //介绍
    @IBOutlet weak var jieshao:UILabel!
    
    //相关动作
    @IBOutlet weak var aboutActionCollectionView:UICollectionView!
    
    //机械推荐
    @IBOutlet weak var machineryCollectionView:UICollectionView!
    
    
    //图片数据
    var pitureDataSource:Array = [MCInfoActionFileList?]()
    
    //推荐动作
    var actionRecommandDataSource:Array = [MCIntrumentRecommandRecord?]()
    
    //推荐器械
    var intrumentRecommandDataSource:Array = [MCInfoRecommandRecord?]()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        
        
        let pictureCollectionFlowLayout = UICollectionViewFlowLayout.init()
        pictureCollectionFlowLayout.scrollDirection = .horizontal
        self.pictureCollectionView.setCollectionViewLayout(pictureCollectionFlowLayout, animated: true)
        self.pictureCollectionView.dataSource = self
        self.pictureCollectionView.delegate = self
        
        self.pictureCollectionView.register(UINib(nibName: "MCPitureInfoCollectionCell", bundle: Bundle.main), forCellWithReuseIdentifier: actionPictureCollectionViewIdentifier)
        
        
        let aboutActionCollectionFlowlayout = UICollectionViewFlowLayout.init()
        aboutActionCollectionFlowlayout.scrollDirection = .horizontal
        self.aboutActionCollectionView.setCollectionViewLayout(aboutActionCollectionFlowlayout, animated: true)
        self.aboutActionCollectionView.dataSource = self
        self.aboutActionCollectionView.delegate = self
        
        self.aboutActionCollectionView.register(UINib(nibName: "MCActionRecommandCollectionCell", bundle: Bundle.main), forCellWithReuseIdentifier: MCActionRecommandCollectionCellIdentifier)
        
        
        let machineryCollectionViewFlowlayout = UICollectionViewFlowLayout.init()
        machineryCollectionViewFlowlayout.scrollDirection = .horizontal
        self.machineryCollectionView.setCollectionViewLayout(machineryCollectionViewFlowlayout, animated: true)
        self.machineryCollectionView.dataSource = self
        self.machineryCollectionView.delegate = self
        
        
        self.machineryCollectionView.register(UINib(nibName: "MCIntrumentRecommandCollectionCell", bundle: Bundle.main), forCellWithReuseIdentifier: MCIntrumentRecommandCollectionCellIdentifier)
        
         
        self.getIntrumentData()
        self.getRecommandAction(instruments: self.ids)
    }
    

    //收藏标签
    @IBAction func collectionSender(_ sender: UIButton) {
        
    }
    
    //分享标签
    @IBAction func shareSender(_ sender: UIButton) {
        
    }
    
    //查看全部动作
    @IBAction func AllActionSender(_ sender: UIButton) {
        
        let allTrainingVc = MCTraningVc.init()
        allTrainingVc.instrumentId = self.ids
        self.navigationController?.pushViewController(allTrainingVc, animated: true)
        
    }
    
    
    
    
    
    //详情页
    func getIntrumentData() -> Void {
        NetWorkRequest(MCActionDetailApi.getInstrumentDetail(ids: self.ids)) { ResponseModel in
            let infoDetilModel = MCInfoActionIntreumentListDetailModel(JSONString: ResponseModel.dataString)
            
            print(infoDetilModel?.firstLetterUpper as Any)
                
            self.pitureDataSource.append(contentsOf: infoDetilModel?.fileList ?? [])
            
            self.titleEnLabel.text =  infoDetilModel?.enName ?? ""
            self.titleEnLabel.text = infoDetilModel?.zhName ?? ""
            
            self.jieshao.text = infoDetilModel?.introduce ?? ""
            
            self.pictureCollectionView.reloadData()
            self.getIntruementsAction(zhName: infoDetilModel?.zhName ?? "")
            
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    ///
    func getRecommandAction(instruments:String) -> Void {
        NetWorkRequest(MCActionDetailApi.getInstrumentInfoActionDetail(current:1, instrumentIds: instruments)) { ResponseModel in
            let recommandList = MCIntrumentRecommandListModel.init(JSONString: ResponseModel.dataString)
            self.actionRecommandDataSource.append(contentsOf: recommandList?.records ?? [])
            self.aboutActionCollectionView.reloadData()
            
        }failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
    //推荐器械
    func getIntruementsAction(zhName:String) -> Void {
        NetWorkRequest(MCActionDetailApi.getInstrumentRecommandDetail(current: 1, zhName: zhName)) { ResponseModel in
            let intruemtnRecommandModel = MCInfoRecommandListInfoModel(JSONString: ResponseModel.dataString)
            self.intrumentRecommandDataSource.append(contentsOf: intruemtnRecommandModel?.records ?? [])
            self.machineryCollectionView.reloadData()
            
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
     
}

extension MCActionMachineryVc:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
   
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 200, height: 150)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.pictureCollectionView {
            return self.pitureDataSource.count
        }else if collectionView == self.aboutActionCollectionView {
            return self.actionRecommandDataSource.count
        }else if collectionView == self.machineryCollectionView {
            return self.intrumentRecommandDataSource.count
        }else {
            return 0
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.pictureCollectionView {
            let cell:MCPitureInfoCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: actionPictureCollectionViewIdentifier, for: indexPath) as! MCPitureInfoCollectionCell
            
            let fileList = self.pitureDataSource[indexPath.row]
            
            cell.headerImageView.kf.setImage(with: URL(string: fileList?.fileUrl ?? ""))
            
            if fileList?.type == 1 {
                cell.isPlayVideoImageView.isHidden = false
            }else{
                cell.isPlayVideoImageView.isHidden = true
            }
            
            
            return cell
        }else if collectionView == self.aboutActionCollectionView {
            let cell:MCActionRecommandCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: MCActionRecommandCollectionCellIdentifier, for: indexPath) as! MCActionRecommandCollectionCell
            let records = self.actionRecommandDataSource[indexPath.row]
            print(records?.previewUrl as Any)
            cell.headerImageView.kf.setImage(with: URL(string: records?.previewUrl ?? ""))
            cell.titleLabel.text = records?.zhName ?? ""
            return cell
        } else{
            let cell:MCIntrumentRecommandCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: MCIntrumentRecommandCollectionCellIdentifier, for: indexPath) as! MCIntrumentRecommandCollectionCell
            
            let recommandIntrumentModel = self.intrumentRecommandDataSource[indexPath.row]
            
            cell.headerImageView.kf.setImage(with: URL(string: recommandIntrumentModel?.previewPic ?? ""))
            cell.titleInfoLabel.text = recommandIntrumentModel?.zhName ?? ""
            return cell
        }
        
    }
    
}
